// ----------------------------------------------------------------------------
// --- Painterly Rendering Plugin for LightWave 3D. -- by Sperl Daniel --------
// --- started development: 2007-01-28 ----------------------------------------
// ----------------------------------------------------------------------------

/*

TODO:

- make proper copy function
- clean up uppercase/lowercase mixture of function-names

*/


#include <lwserver.h>
#include "plugin.h"

ServerRecord ServerDesc[] = {
   { LWIMAGEFILTER_HCLASS, PLUGIN_NAME,    BWActivate },
   { LWIMAGEFILTER_ICLASS, PLUGIN_NAME,    BWUiActivate },
   { LWCUSTOMOBJ_HCLASS,   PLUGIN_CO_NAME, BWcoActivate },
   { LWCUSTOMOBJ_ICLASS,   PLUGIN_CO_NAME, BWcoUiActivate },
   { NULL }
};

// ----------------------------------------------------------------------------
void *Startup (void)
{
    WriteTrace("--- Startup called ---------------------------------");

    // create global array that will contain pointers to all instance datas
    // of the custom objects.
    bwCoInstances = calloc(MAX_NUM_OBJECTS, sizeof(BWcoInstanceData*));   

    // this is the default behaviour for success.
    if (bwCoInstances)
        return (void *) 4;
    else 
        return NULL;
}

// ----------------------------------------------------------------------------
void Shutdown (void* serverData)
{
    int i;
    
    if (bwCoInstances) 
    {        
        for (i=0; i<MAX_NUM_OBJECTS; ++i)
        {
            if (bwCoInstances[i]) 
            {
                free(bwCoInstances[i]);
                bwCoInstances[i] = NULL;
            }
        }
        
        free(bwCoInstances);
        bwCoInstances = NULL;
    }
        
    WriteTrace("--- Shutdown finished ------------------------------");
}

// ----------------------------------------------------------------------------
void AddToGlobalList(BWcoInstanceData* instance)
{
    int i;

    for (i=0; i<MAX_NUM_OBJECTS; ++i)
    {        
        if (bwCoInstances[i] == instance) 
        {
            // already there!
            return;
        }   
        else if (bwCoInstances[i] == NULL)        
        {
            // new item!
            bwCoInstances[i] = instance;
            return;
        }
        else if (bwCoInstances[i]->itemID == instance->itemID) 
        {
            WriteTrace("In AddToGlobalList: item existed already, replacing it");
            free(bwCoInstances[i]);
            bwCoInstances[i] = instance;
            return;
        }
    }

    WriteTrace("Could not add instance! Already added %d objects!", MAX_NUM_OBJECTS);
}

// ----------------------------------------------------------------------------
BWcoInstanceData* GetFromGlobalList(LWItemID itemID)
{
    int i;
    
    for (i=0; i<MAX_NUM_OBJECTS; ++i)
    {
        // NULL entry reached -> item not found
        if (bwCoInstances[i] == NULL) return NULL;

        // found entry!
        if (bwCoInstances[i]->itemID == itemID) return bwCoInstances[i];
    }

    // list full, entry not found.
    return NULL;
}

// ----------------------------------------------------------------------------
void RemoveFromGlobalList(LWItemID itemID)
{
    // we don't really remove the element, but set the ItemID to NULL.
    BWcoInstanceData* instance = GetFromGlobalList(itemID);    
    instance->itemID = LWITEM_NULL;
}